package com.tanovo.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tanovo.bo.CompanyQueryBo;
import com.tanovo.bo.ResultObject;
import com.tanovo.bo.UserCore;
import com.tanovo.config.UserInfoContext;
import com.tanovo.domain.Company;
import com.tanovo.service.ICompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "公司管理")
@Slf4j
public class CompanyApi {
    @Autowired
    private ICompanyService iCompanyService;

    @PostMapping("/company/add")
    @ApiOperation(value = "添加接入的公司", notes = "添加接入的公司")
    public ResultObject addCompany(@RequestBody Company company) {
        UserCore userCore = UserInfoContext.getUser();
        company.setCreateBy(userCore.getId().toString());
        company.setUpdateBy(userCore.getId().toString());
        iCompanyService.addPartnerCompany(company);
        return new ResultObject();
    }

    @PostMapping("/company/delete")
    @ApiOperation(value = "删除公司list", notes = "删除公司list")
    public ResultObject delete(@RequestBody List<Integer> companyIdList) {
        iCompanyService.delCompanyList(companyIdList);
        return new ResultObject();
    }

    @PostMapping("/company/update")
    @ApiOperation(value = "修改编辑公司", notes = "修改编辑公司")
    public ResultObject editCompany(@RequestBody Company company) {
        UserCore userCore = UserInfoContext.getUser();
        company.setUpdateBy(userCore.getId().toString());
        iCompanyService.editCompany(company);
        return new ResultObject();
    }

    @GetMapping("/company/detail")
    @ApiOperation(value = "查找公司详情", notes = "查找公司详情")
    public ResultObject<Company> detail(@RequestParam Integer companyId) {
        Company company = iCompanyService.detail(companyId);
        return new ResultObject(company);
    }

    @PostMapping("/company/list")
    @ApiOperation(value = "查找公司列表", notes = "查找公司列表")
    public ResultObject<List<Company>> list(@RequestBody CompanyQueryBo companyQueryBo) {
        PageHelper.startPage(companyQueryBo.getPageNum(), companyQueryBo.getPageSize());
        List<Company> list = iCompanyService.list(companyQueryBo);
        PageInfo<Company> pageInfo = new PageInfo<>(list);
        return new ResultObject(pageInfo);
    }

    @GetMapping("/company/detail/token")
    @ApiOperation(value = "查找当前登录人公司详情", notes = "查找当前登录人公司详情")
    public ResultObject<Company> detailByToken() {
        UserCore user = UserInfoContext.getUser();
        Company company = iCompanyService.detail(user.getCompanyId());
        return new ResultObject(company);
    }

    @GetMapping("/company/shareCode")
    @ApiOperation(value = "分享码查找公司", notes = "分享码查找公司")
    public ResultObject<Company> getCompanyByShareCode(@RequestParam String shareCode) {
        Company company = iCompanyService.getCompanyByShareCode(shareCode);
        return new ResultObject(company);
    }

    @GetMapping("/company/bindUser")
    @ApiOperation(value = "公司绑定员工", notes = "公司绑定员工")
    public ResultObject companyBindUser(@RequestParam Integer companyId) {
        iCompanyService.companyBindUser(companyId, UserInfoContext.getUser().getId());
        return new ResultObject();
    }

}
